package com.server.handler;

import com.codec.AppConfigDecoder;
import com.codec.AppConfigEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ServerInitializer extends ChannelInitializer<SocketChannel>{

    @Autowired
    TcpHandler tcpHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decoder",new AppConfigDecoder());
        pipeline.addLast("encoder",new AppConfigEncoder());
//        pipeline.addLast(new DefaultHttpRequest());
        pipeline.addLast(new WebSocketServerProtocolHandler("/netty"));
        pipeline.addLast("handler",tcpHandler);
    }
}
